package com.senyint.hip.fhir.web;

import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.impl.GenericClient;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.StringClientParam;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.hl7.fhir.r4.model.Bundle;
import org.springframework.web.bind.annotation.*;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author ChenPan
 * @date 2018/10/30 10:50
 */
@RestController
@Slf4j
public class FhirClientController extends FhirBaseController {

    @PutMapping("/server")
    public String setServer(@RequestParam("server") String server) {
        tempData.setBaseServer(server);
        return "";
    }

    @GetMapping("/search")
    public String searchResource(@RequestParam("type") String type,
                                @RequestParam(value = "paramName", required = false) String paramName,
                                @RequestParam(value = "value", required = false) String value,
                                @RequestParam(value = "id", required = false) String id) {
        log.debug("server:{}", tempData.getBaseServer());

        GenericClient client = (GenericClient) ctx.newRestfulGenericClient(tempData.getBaseServer());

        MethodOutcome out = null;
        IQuery query = client.search().forResource(type);
           if (id != null && !"".equals(id)) {
        	String json = new Gson().toJson(client.read().resource(type).withId(id).execute());
        	  System.out.println(json);
            return new Gson().toJson(client.read().resource(type).withId(id).execute());
        } else  if (value != null && !"".equals(value) && paramName != null && !"".equals(paramName)) {
               return new Gson().toJson(client.search().forResource(type).where
                       (new StringClientParam(paramName).matches().value(value))
                       .returnBundle(Bundle.class).execute());
           } else  {
        	String  aa=new Gson().toJson(client.search().forResource(type).returnBundle(Bundle.class).execute());
            System.out.println(aa);
        	return new Gson().toJson(client.search().forResource(type).returnBundle(Bundle.class).execute());
        }

    }

    @DeleteMapping("/delete")
    public String delResource(@RequestParam("type") String type,
                             @RequestParam("id") String id) {

        GenericClient client = (GenericClient) ctx.newRestfulGenericClient(tempData.getBaseServer());

        return new Gson().toJson(client.delete().resourceById(type, id).execute());
    }


    @GetMapping("/history")
    public String historyResource(@RequestParam("type") String type,
                                  @RequestParam("id") String id) {

        GenericClient client = (GenericClient) ctx.newRestfulGenericClient(tempData.getBaseServer());

        return new Gson().toJson(client.history()
                .onInstance(new IdDt(type, id))
                .andReturnBundle(Bundle.class)
                .execute());
    }
}
